﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace ExcelImport.Validators
{
    public class DataLengthValidator : IDataRowProcessor
    {
        public void Run(DataRow row, PipelineContext context)
        {
            foreach (var importProperty in context.ImportTable.ImportProperties.Where(p => p.Ordinal.HasValue && p.IsCheckLength))
            {
                string value = row[importProperty.Ordinal.Value]?.ToString();

                if (importProperty.MinLength.HasValue && value.Length < importProperty.MinLength.Value)
                {
                    context.AddValidationResult(new ValidationResult($"第{context.Index}行,{importProperty.HeaderName}不能小于{importProperty.MinLength.Value}位"));
                }
                if (importProperty.MaxLength.HasValue && value.Length > importProperty.MaxLength.Value)
                {
                    context.AddValidationResult(new ValidationResult($"第{context.Index}行,{importProperty.HeaderName}不能大于{importProperty.MaxLength.Value}位"));
                }

            }

        }
    }
}
